package mule;

import java.awt.Color;
import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

/**
 * Player objects for the MULE game
 */
public class Player implements Serializable {
	private int money;
	private int turnTime = 50;// set time to 30 if food<requirement,
	private int score; // set to 5 if food=0
	private int food;
	private int energy;
	private int ore;
	private int race;
	private int crystite;
	private Mule mule;
	private Color color;
	private String name;
	private ArrayList<Tile> ownedTiles;
	private Random rand;

	/**
	 * Construct player with loaded stats
	 */
	public Player(int money, int turnTime, int score, int food, int energy,
			int ore, int race, int crystite, Color color, String name,
			ArrayList<Tile> ownedTiles) {
		this.money = money;
		this.turnTime = turnTime;
		this.score = score;
		this.food = food;
		this.energy = energy;
		this.ore = ore;
		this.race = race;
		this.crystite = crystite;
		this.color = color;
		this.name = name;
		this.ownedTiles = ownedTiles;
		rand = new Random();
	}

	/**
	 * Construct a player based on race, difficulty, and name Difficulties:
	 * 1-Beginner, 2-Standard, 3-Tournament Races 1-Flapper, 2-4 Other Aliens,
	 * 3-Human
	 */
	public Player(int difficulty, int race, Color color, String name) {
		this.race = race;
		this.color = color;
		this.name = name;
		this.ownedTiles = new ArrayList<Tile>();
		this.score = 0;
		this.ore = 0; // same for all difficulties
		this.mule = null;
		if (race == 1) {// flapper
			this.money = 1600;
		} else if (race == 5) {// human
			this.money = 600;
		} else {// other aliens 2-4
			this.money = 1000;
		}
		if (difficulty == 1) {// Beginner
			this.food = 8;
			this.energy = 4;
		} else {// Standard or Tournament (same values)
			this.food = 4;
			this.energy = 2;
		}
		rand = new Random();
	}

	/**
	 * add a Tile to the owned list of tiles
	 */
	public void addTile(Tile tile) {
		ownedTiles.add(tile);
	}

	public void addTile(Point loc) {
		Tile[] tiles = Map.getMap();
		for (Tile t : tiles) {
			if (loc.equals(t.getPoint())) {
				ownedTiles.add(t);
				t.setOwner(this);
			}
		}
	}

	public void gamble(int time, int round) {
		int timeBonus, roundBonus;
		if (time > 37) {
			timeBonus = rand.nextInt(200);
		} else if (Game.time > 25) {
			timeBonus = rand.nextInt(150);
		} else if (Game.time > 12) {
			timeBonus = rand.nextInt(100);
		} else {
			timeBonus = rand.nextInt(50);
		}

		if (round < 4) {
			roundBonus = 50;
		} else if (round < 8) {
			roundBonus = 100;
		} else if (round < 12) {
			roundBonus = 150;
		} else {
			roundBonus = 200;
		}
		if (timeBonus + roundBonus > 250) {
			setMoney(getMoney() + 250);
		} else {
			setMoney(getMoney() + timeBonus + roundBonus);
		}
	}

	/**
	 * Getters and Setters for player variables
	 */
	public void setFood(int food) {
		this.food = food;
	}

	public int getFood() {
		return food;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public int getMoney() {
		return money;
	}

	public void setTurnTime(int time) {
		this.turnTime = time;
	}

	public int getTurnTime() {
		return turnTime;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScore() {
		return score;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public ArrayList<Tile> getOwnedTiles() {
		return ownedTiles;
	}

	public Color getColor() {
		return color;
	}

	public int getRace() {
		return race;
	}

	public void setOre(int ore) {
		this.ore = ore;
	}

	public int getOre() {
		return ore;
	}

	public void setEnergy(int energy) {
		this.energy = energy;
	}

	public int getEnergy() {
		return energy;
	}

	public int getCrystite() {
		return crystite;
	}

	public void setCrystite(int crystite) {
		this.crystite = crystite;
	}

	public Mule getMule() {
		return mule;
	}

	public void setMule(Mule mule) {
		this.mule = mule;
	}

	public boolean hasMule() {
		if (mule == null)
			return false;
		return true;
	}
}
